package com.wondersgroup.insurance.dnuilearn.recyclerview.recycledviewpool;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wondersgroup.insurance.dnuilearn.R;

import java.util.Arrays;
import java.util.Random;

/**
 * Created by LK on 2017/6/18.
 * 动脑学院 版权所有
 */

public class DemoAdapter extends RecyclerView.Adapter {

    String[] mTexts;
    DemoAdapter(){
        int count = new Random().nextInt(60);
        mTexts = new String[count];
        fillTexts(count);
    }

    private void fillTexts(int count) {
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            char[] buf = new char[random.nextInt(10)];
            Arrays.fill(buf, (char) (random.nextInt(26) + 65));
            mTexts[i] = new String(buf);
        }
    }
    static final int TEXT = 0;
    static final int TITLE = 1;

    @Override
    public int getItemViewType(int position) {
        if (position % 10 == 0) return TITLE;
        return TEXT;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        TextView view = new TextView(viewGroup.getContext());
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        if (viewType == TITLE) {
            return new TitleTextViewHolder(view);
        } else {
            return new TextViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        TextView itemView = (TextView) viewHolder.itemView;
        if(viewHolder instanceof TitleTextViewHolder) {
            itemView.setText("~~~"+mTexts[position]+"~~~~");
        }else{
            itemView.setText(position+". "+mTexts[position]);
        }
    }

    @Override
    public int getItemCount() {
        return mTexts.length;
    }

    static class TextViewHolder extends RecyclerView.ViewHolder{

        public TextViewHolder(TextView itemView) {
            super(itemView);
            itemView.setTextAppearance(itemView.getContext(), R.style.TextAppearance_AppCompat_Medium);
        }
    }

    static class TitleTextViewHolder extends RecyclerView.ViewHolder{

        public TitleTextViewHolder(TextView itemView) {
            super(itemView);
            itemView.setTextAppearance(itemView.getContext(), R.style.TextAppearance_AppCompat_Headline);
            itemView.setGravity(Gravity.CENTER_HORIZONTAL);
        }
    }
    /**
     * 对GridLayoutManager的处理；该方法会在RecyclerView.setAdapter()方法中被调用，因此前面建议保证一定在setLayoutManager方法之后调用该方法
     * @param recyclerView
     */
 /*   @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    switch(mAdapter.getItemViewType(position)){
                        case MyAdapter.TYPE_HEADER:
                            return 2;
                        case MyAdapter.TYPE_ITEM:
                            return 1;
                        default:
                            return -1;
                    }
                }
            });
        }
    }*/
    /**
     * 对StaggeredGridLayoutManager的处理
     * @param holder
     */
    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        if(layoutParams!=null&& layoutParams instanceof StaggeredGridLayoutManager.LayoutParams ){
            StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) layoutParams;
            int position = holder.getLayoutPosition();
//            if: positon是Header视图或Footer视图显示的位置
//            lp.setFullSpan(true);
        }
    }


}
